package sparrowGui.component
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	
	import sparrowGui.SparrowGUI;
	
	/**
	 * 鼠标经过弹出提示(文本提示,固定宽,自动高)
	 * @author Pelephone
	 */
	public class ToolTip extends BaseTip
	{
		/**
		 * 提示窗里的文本
		 */
		private static const TXTTIP_NAME:String = "txtTip";
		/**
		 * 提示窗里的背景
		 */
		private static const SKINBG_NAME:String = "skinBG";
		
		/**
		 * 响应鼠标的对象哈希
		 */
		private var targetHash:Dictionary;
		
		/**
		 * 文本的边距
		 */
		private var padding:int = 5;
		
		/**
		 * 构造鼠标提示窗
		 * @param parentSkin
		 * @param argSkin
		 */
		public function ToolTip(parentSkin:DisplayObjectContainer, argSkin:Sprite=null)
		{
			super(parentSkin, argSkin);
		}
		
		override protected function create(argSkin:Sprite):void
		{
			targetHash = new Dictionary(true);
			argSkin = argSkin || SparrowGUI.getIns().toolTipSkin;
			
			super.create(argSkin);
			
			txtTip.addEventListener(TextEvent.TEXT_INPUT,onTxtChange);
			txtTip.addEventListener(Event.SCROLL,onTxtChange);
		}
		
		override public function show(data:Object=null, tarDisp:DisplayObject=null):void
		{
			super.show(data,tarDisp);
			
			tarDisp.addEventListener(MouseEvent.MOUSE_OUT,onOutEvt);
		}
		
		/**
		 * 鼠标移开响应
		 */
		private function onOutEvt(e:Event):void
		{
//			e.currentTarget.removeEventListener(e.type, arguments.callee);
			e.currentTarget.addEventListener(MouseEvent.MOUSE_OUT,onOutEvt);
			clearUp();
		}
		
		/**
		 * 给对象增加提示
		 * @param tarDisp
		 * @param data
		 */
		public function addTip(tarDisp:DisplayObject,data:String=null):void
		{
			targetHash[tarDisp] = data;
			tarDisp.addEventListener(MouseEvent.MOUSE_OVER,onOverEvt);
		}
		
		/**
		 * 移出提示事件
		 * @param tarDisp
		 */
		public function removeTip(tarDisp:DisplayObject):void
		{
			delete targetHash[tarDisp];
			tarDisp.removeEventListener(MouseEvent.MOUSE_OVER,onOverEvt);
		}
		
		/**
		 * 移出所有提示事件
		 * @param tarDisp
		 */
		public function removeAllTip():void
		{
			for (var tarDisp:Object in targetHash) 
			{
				removeTip(tarDisp as DisplayObject);
			}
		}
		
		override public function update(data:Object):void
		{
			txtTip.htmlText = String(data);
			draw();
			super.update(data);
		}
		
		/**
		 * 重绘
		 */
		protected function draw():void
		{
			txtTip.x = txtTip.y = padding;
			skinBG.width = txtTip.width + padding*2;
			skinBG.height = txtTip.height + padding*2;
		}
		
		/**
		 * 监听鼠标事件
		 * @param e
		 */
		private function onOverEvt(e:MouseEvent):void
		{
			var data:String = targetHash[e.currentTarget];
			show(data,(e.currentTarget as DisplayObject));
		}
		
		/**
		 * 文本框改变,背景也跟着变化
		 * @param e
		 */
		private function onTxtChange(e:Event=null):void
		{
			draw();
		}
		
		/**
		 * 提示窗宽
		 * @param value
		 */
		public function set width(value:Number):void 
		{
			txtTip.width = value;
			onTxtChange();
		}
		
		public function get width():Number 
		{
			return skin.width;
		}
		
		/**
		 * 提示窗高
		 * @param value
		 */
		public function set height(value:Number):void 
		{
			txtTip.height = value;
			onTxtChange();
		}
		
		public function get height():Number 
		{
			return skin.height;
		}
		
		public function get txtTip():TextField
		{ return skin.getChildByName(TXTTIP_NAME) as TextField; }
		public function get skinBG():DisplayObject
		{ return skin.getChildByName(SKINBG_NAME); }
	}
}